﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RMS.Utils.MVP;
using RMS.Web.Shell.View;
using RMS.DataAccess.Entities;
using RMS.DataAccess.Repositories;
using RMS.Utils.Authentication;
using RMS.DataAccess.Common;

namespace RMS.Web.Shell.Presenter
{
    public class EditReportPresenter : Presenter<IEditReportView>
    {
        IAuthentication _authentication;
        RadiologyImageRepository _radiologyImageRepository;
        RadiologyReportRepository _radiologyReportRepository;
        RadiologyRequestRepository _radiologyRequestRepository;
        PatientsRepository _patientsRepository;

        public EditReportPresenter(IAuthentication authentication,
            RadiologyImageRepository radiologyImageRepository, RadiologyReportRepository radiologyReportRepository,
            RadiologyRequestRepository radiologyRequestRepository, PatientsRepository patientsRepository)
        {
            _authentication = authentication;
            _radiologyImageRepository = radiologyImageRepository;
            _radiologyReportRepository = radiologyReportRepository;
            _radiologyRequestRepository = radiologyRequestRepository;
            _patientsRepository = patientsRepository;
        }

        public RadiologyRequest CurrentRequest(int RequestID)
        {
            return _radiologyRequestRepository.Find(item => item.RequestId = RequestID).Single();
        }
        public string[] GetImages(int requestID)
        {
            List<string> pathes = new List<string>();
            EntityResult<RadiologyImage> images = _radiologyImageRepository.Find(item => item.RequestId = requestID);
            foreach (RadiologyImage img in images.Rows)
            {
                pathes.Add(img.ImagePath.ToVirtualPath());
            }
            return pathes.ToArray();
        }
        public RadiologyReport GetReport(int ReqestId)
        {
            try
            {
                return _radiologyReportRepository.Find(item => item.RequestID = ReqestId).Single();
            }
            catch (Exception)
            {
                return new RadiologyReport();
            }
        }
        public EntityResult<RadiologyRequest> GetRequest(int patientId)
        {
            return _radiologyRequestRepository.Find(item => item.PatientID = patientId);
        }
        public EntityResult<RadiologyRequest> GetRequest(String patientName)
        {
            string[] names = patientName.Split(' ');
            var result = _patientsRepository.Find(items =>
            {
                items.FirstName = names[0];
                if (names.Count() > 1)
                    items.FamilyName = names[1];
            }).Single();

            return _radiologyRequestRepository.Find(item => item.PatientID = result.PatientID);

        }
        public Patients GetPationt(int patientID)
        {
            return _patientsRepository.Find(item => item.PatientID = patientID).Single();
        }
        public void UpdateReport(RadiologyReport radiologyReport)
        {
            _radiologyReportRepository.Update(radiologyReport);
        }

    }
}
